package org.example.seat_management.Service.Impl;

import org.example.seat_management.Entity.SeatConfig;
import org.example.seat_management.Mapper.SeatConfigMapper;
import org.example.seat_management.Service.SeatConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SeatConfigServiceImpl implements SeatConfigService {

    private final SeatConfigMapper seatConfigMapper;

    public SeatConfigServiceImpl(SeatConfigMapper seatConfigMapper) {
        this.seatConfigMapper = seatConfigMapper;
    }

    @Override
    @Transactional
    public Long create(SeatConfig config) {
        // 幂等：若唯一键冲突，改为 upsert 保持一条生效配置
        try {
            seatConfigMapper.insert(config);
        } catch (Exception e) {
            seatConfigMapper.upsert(config);
        }
        return config.getId();
    }

    @Override
    @Transactional
    public void update(SeatConfig config) {
        seatConfigMapper.update(config);
    }

    @Override
    public SeatConfig getActiveByClassroom(Long classroomId) {
        return seatConfigMapper.findActiveByClassroom(classroomId);
    }

    @Override
    public List<SeatConfig> listByClassroom(Long classroomId) {
        return seatConfigMapper.findByClassroom(classroomId);
    }
}


